#@layoutT("销售单")
#define main()
<form class="layui-form layui-form-pane f-form" id='searchForm' lay-filter='searchForm' action="javascript:;">
	<div class='layui-row layui-col-space1 task-row'>
		<div class='layui-col-xs12 '>
			<div class="layui-form-item">
				#@queryStart('开单人')
				<select id="userName" name="userName" class="layui-input" lay-search>
					<option value="">---请选择开单人---</option>
					#for(data:originatorList)
					<option value="#(data.value??)">#(data.text??)</option>
					#end
				</select>
				#@queryEnd()
				#@queryStart('客户')
				<input type="search" id="customerName" name="customerName" autocomplete="off" class="layui-input" placeholder="客户"/>
				#@queryEnd()
				#@queryStart('开始时间')
				   <input id="startTime" type="search" name="startTime" autocomplete="off" class="layui-input" placeholder="开始时间（开单时间）"/>
				#@queryEnd()
				#@queryStart('结束时间')
				<input id="endTime" type="search" name="endTime" autocomplete="off" class="layui-input" placeholder="结束时间（开单时间）"/>
				#@queryEnd()
				#@queryStart('打印状态')
				<select id="status" name="status" class="layui-input" lay-search>
					<option value="">---请选择打印状态---</option>
					<option value="1000">未打印</option>
					<option value="1100">已打印</option>
				</select>
				#@queryEnd()
				<div class="layui-inline">
					<div class="layui-btn-group">
						<button class="layui-btn layui-btn-disabled" disabled id="searchBtn_">
							<i class="layui-icon">&#xe615;</i>查询
						</button>
						<button class="layui-btn" id="exportBtn">
							<i class="layui-icon">&#xe67d;</i>导出
						</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</form>
	<div class='layui-row f-index-toolbar'>
		<div class='layui-col-xs12'>
			<div class="layui-btn-group">
				<button id='addBtn_' class="layui-btn layui-btn-normal layui-btn-sm">
					<i class="layui-icon">&#xe608;</i> 新增
				</button>
				<button id='deleteBtn_' class="layui-btn  layui-btn-normal layui-btn-sm">
					<i class="layui-icon">&#xe640;</i> 删除
				</button>
				<button id='approve' class="layui-btn layui-btn-normal layui-btn-sm">
					<i class="layui-icon">&#xe605;</i> 确认
				</button>
				<button id='refuse' class="layui-btn  layui-btn-normal layui-btn-sm">
					<i class="layui-icon">&#x1006;</i> 退回
				</button>
			</div>
		</div>
	</div>
	<div class="layui-row  f-index-toolbar">
			<div class="layui-col-xs12">
   				<table id="maingrid" lay-filter="maingrid"></table>
   			</div>
   	</div>
   	
   	<!-- 	每行的操作按钮 -->
	<script type="text/html" id="bar_maingrid">
		<a class="layui-btn layui-btn-xs" lay-event="printTemp">打印</a>
		<!--<a class="layui-btn layui-btn-xs" lay-event="showOrderDetailList">订单项</a>-->
		<!--<a class="layui-btn layui-btn-xs" lay-event="showIncomeList">付款明细</a>-->
		<a class="layui-btn layui-btn-xs" lay-event="review">订单信息</a>
  		<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
		<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
	</script>
#end

<!-- 时间控件 -->
#define layuiFunc()
    laydate.render({
        elem: '#startTime',
        type: 'datetime'
    });
    laydate.render({
        elem: '#endTime',
        type: 'datetime'
    });
#end

#define js()
<!-- 分页表格 -->
<script>
	$(function () {
        getTable();

        $("#exportBtn").click(function(){
        	debugger;
        	var userName = $('#userName').val();
        	var startTime = $('#startTime').val();
        	var endTime = $('#endTime').val();
        	var status = $('#status').find('option:selected').val();
        	var customerName = $('#customerName').val();
        	location.href = '#(path)/erp/order/sell/export?userName='+userName+'&startTime='+startTime+'&endTime='+endTime+'&status='+status+'&customerName='+customerName;
        });
    })

	function getTable(){
        gridArgs.title='销售单列表';
        gridArgs.dataId='order_id';
        gridArgs.deleteUrl='#(path)/erp/order/sell/delete';
        gridArgs.updateUrl='#(path)/erp/order/sell/edit?orderId=';
        gridArgs.addUrl='#(path)/erp/order/sell/add';
        gridArgs.gridDivId ='maingrid';
        gridArgs.approveUrl = '#(path)/erp/order/sell/approve';
        gridArgs.approveBtn='#approve';
        gridArgs.refuseUrl = '#(path)/erp/order/sell/refuse';
        gridArgs.refuseBtn='#refuse';
        initGrid({id : 'maingrid'
            ,elem : '#maingrid'
            ,cellMinWidth: 80
            ,cols : [ [
                {title: '主键',field : 'order_id',width : 35,checkbox : true},
                {title:'序号',type:'numbers',width:35},
                {title: '客户', field: 'customer_name',width:100,sort:true },
                {title: '开单人', field: 'originator_name',width:100,sort:true},
                {title: '状态', field: 'status',sort:true,width:100,templet:statusStr},
                <!--{title: '经手人', field: 'hander_name'},-->
                <!--{title: '确认人', field: 'confirmer_name'},-->
                {title: '开单时间', field: 'originat_time',sort:true,width:180},
                {title: '发货时间', field: 'plan_time',sort:true,width:180},
                <!--{title: '实际到货时间', field: 'actual_time'},-->
                {title: '结账方式', field: 'settle_way',templet:settleStr,sort:true,width:120},
                {title: '总金额', field: 'total_amount',sort:true,width:100},
                {title: '含发票', field: 'contain_bill',templet:'#containBillStr',sort:true,width:100},
                {title: '税率', field: 'tax_rate',sort:true,width:100},
                {title: '物流单号', field: 'logistics_num',width:120},
                {title: '运费类型', field: 'carriage_type',templet:carriageTypeStr,sort:true,width:120},
                {title: '运费', field: 'carriage',sort:true},
                {title: '实际运费', field: 'real_carriage',sort:true,width:120},
                {title: '操作',fixed:'right',width : 240,align : 'left',toolbar : '#bar_maingrid'} // 这里的toolbar值是模板元素的选择器
            ] ]
            ,url:"#(path)/erp/order/sell/list?originator="+$('#userCode').val()
            ,where:{"object":"sell_order","primaryKey":gridArgs.dataId}
            ,searchForm : 'searchForm'
        },{
            showOrderDetailList:showOrderDetailList,
			review:review,
			showIncomeList:showIncomeList,
			printTemp:printTemp
        });
	}

	function review(obj) {
        var data=obj.data;
        var id=data[gridArgs.dataId];
        openDialog("销售单预览窗口","#(path)/erp/order/sell/review?orderId="+id,false,null,null,null);
    }

    function showOrderDetailList(obj){
        var data=obj.data;
        var id=data[gridArgs.dataId];
        openDialog("订单明细窗口","#(path)/erp/order/sellDetail/index?orderId="+id,false,null,null,null);
    }

    function showIncomeList(obj){
        var data=obj.data;
        var id=data[gridArgs.dataId];
        openDialog("付款明细窗口","#(path)/erp/order/incomeDetail/index?orderId="+id,false,null,null,null);
    }

    function printTemp(obj){
    	var data=obj.data;
    	var id=data[gridArgs.dataId];
    	openDialogPrint(id,"打印预览窗口","#(path)/erp/order/sell/printTemp?orderId="+id,false,null,null,null);
    }

 function openDialogPrint(orderId,dialogTitle,dialogUrl,isMax,w,h,option,renderView){
    var windowHeight = $(window).height();
    var height_=Math.floor(windowHeight*0.9)+'px';
    var width_='90%';
    if(h){
        if(typeof h=='string'){
            height_=h;
        }else{
            height_=h+'px';
        }
    }
    if(w){
        if(typeof w=='string'){
            width_=w;
        }else{
            width_=w+'px';
        }
    }
    var option_=null;
    if(option){
        option_=option;
    }
    var tableOptions = $.extend({
        type: 2,
        maxmin : true,
        title: dialogTitle,
        area: [width_,height_],
        content: dialogUrl,
        btn: ['打印','关闭'],
        yes: function(index){
			//当点击‘确定’按钮的时候，获取弹出层返回的值
			var res = window["layui-layer-iframe" + index].callbackdata();
			//打印返回的值，看是否有我们想返回的值。
			console.log(res);
        	// 点击打印按钮，判断单据状态，若为已打印，弹出"是否再次打印单据？"窗口
			$.ajax({
                type: 'post',
                url: '#(path)/erp/order/sell/getInfo',
                data: {
                    orderId:orderId
                },
                async: false,
                dataType: 'json',
                success: function (result) {
                	debugger;
                	console.log(result.status);
                	if(result.status == "1100"){
                		// 弹窗确认是否再次打印
                		layer.confirm('确定再次打印吗？',{icon: 3, title:'提示'},function(index){
							// 确认按钮事件
							var curIndex = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
							parent.layer.close(curIndex); //再执行关闭
							printTableTemp(res);

                		},function(){
                			// 取消按钮事件
                			debugger;
                			var curIndex = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
							parent.layer.close(curIndex); //再执行关闭
							return;
                		});
                	}else if(result.status == "1000"){
                		updatePrintStatus(orderId); // 更新打印状态
                		printTableTemp(res);
                	}

                }
            });

        },
        cancel: function(){
            //右上角关闭回调
        }
    },option_);
    currentLayer=layer.open(tableOptions);
    if(isMax){
        layer.full(currentLayer);
    }
    return currentLayer;
 }

function printTableTemp(res){
	var bdhtml=window.document.body.innerHTML;
	window.document.body.innerHTML= res;
	//调用打印功能
	window.print();
	location.href = '#(path)/erp/order/sell/index';
}

 // 更新单据状态
 function updatePrintStatus(orderId){
	$.ajax({
		type: 'post',
		url: '#(path)/erp/order/sell/updatePrintStatus',
		data: {
			orderId:orderId
		},
		async: false,
		dataType: 'json',
		success: function (result) {
			debugger;
			console.log(result);
		}
     });
 }
 // 状态，表格中文转义
 function statusStr(data) {
	var status = data.status;
	var btns = "";
	if (status == '1000') {
		return "未打印";
	}
	if (status == '1100') {
		btns += '<a class="" style="color:#ed2a4a">已打印</a>';
	}
	return btns;
 }
 // 结算方式，表格中文转义
 function settleStr(data){
 	var settle = data.settle_way;
 	if(settle == '0'){
 		return '现结';
 	}else if(settle == '1'){
 		return '月结';
 	}else{
 		return data.settle_way;
 	}
 }
 // 运费类型，表格中文转义
 function carriageTypeStr(data){
 	var carriageType = data.carriage_type;
 	if(carriageType == '0'){
 		return '到付';
 	}else if(carriageType == '1'){
 		return '现付';
 	}else if(carriageType == '2'){
 		return '月结';
 	}else{
 		return data.carriage_type;
 	}
 }

</script>
<script type="text/html" id="containBillStr">
	{{ d.contain_bill == 1 ? '是' : '否' }}
</script>


#end

 
 